package com.neulogistics.core.substation.api.application.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neulogistics.core.substation.api.application.converter.SubstationPOVMConverter;
import com.neulogistics.core.substation.api.application.converter.SubstationPaymentPOVMConverter;
import com.neulogistics.core.substation.api.application.converter.SubstationPaymentQueryPOConverter;
import com.neulogistics.core.substation.api.application.converter.SubstationQueryPOConverter;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationPaymentViewModel;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationViewModel;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPO;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPaymentPO;
import com.neulogistics.core.substation.infrastructure.persistence.orm.service.ISubstationPaymentPOService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubstationPaymentQueryService implements ISubstationPaymentQueryService {
    @Resource
    ISubstationPaymentPOService substationPaymentPOService;

    @Override
    public SubstationPaymentViewModel getById(Long id) {
        SubstationPaymentPO substationPaymentPO = substationPaymentPOService.getById(id);
        SubstationPaymentViewModel substationPaymentViewModel = SubstationPaymentPOVMConverter.POToVM(substationPaymentPO);
        return substationPaymentViewModel;
    }

    @Override
    public Page<SubstationPaymentViewModel> page(SubstationPaymentQuery substationPaymentQuery) {
        // 取出条件
        SubstationPaymentPO substationPaymentPO = SubstationPaymentQueryPOConverter.queryToPO(substationPaymentQuery);
        long current = substationPaymentQuery.getPageNum();
        long pageSize = substationPaymentQuery.getPageSize();

        // 查询
        QueryWrapper<SubstationPaymentPO> queryWrapper = new QueryWrapper<>(substationPaymentPO);
        Page<SubstationPaymentPO> page = substationPaymentPOService.page(new Page<>(current, pageSize), queryWrapper);

        // 构造结果
        Page<SubstationPaymentViewModel> viewModelPage = new Page<>(current, pageSize);
        List<SubstationPaymentViewModel> viewModelList = page.getRecords().stream()
                .map(SubstationPaymentPOVMConverter::POToVM).collect(Collectors.toList());
        viewModelPage.setRecords(viewModelList);
        viewModelPage.setTotal(page.getTotal());
        return viewModelPage;
    }
}
